package com.aitaoxue.palx.db;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.aitaoxue.palx.AppContext;

public class InfoDBHelper {

	private static final int DB_VERSION = 1;
	private static final String DB_PATH = "/data/data/"
			+ AppContext.getInstance().getPackageName() + "/databases/";

	private static String DB_NAME = "address.db";
	private static String ASSETS_NAME = "address.db";

	public static void initDatabase() {
		boolean dbExist = checkDataBase();

		if (!dbExist) {
			try {
				File dir = new File(DB_PATH);
				if (!dir.exists()) {
					dir.mkdirs();
				}

				File dbf = new File(DB_PATH + DB_NAME);
				if (dbf.exists()) {
					dbf.delete();
				}
				SQLiteDatabase.openOrCreateDatabase(dbf, null);

				try {
					copyDataBase();
				} catch (IOException e) {
					//
				}

			} catch (Exception e) {
				// TODO: handle exception
			}
		}
	}

	private static boolean checkDataBase() {
		File dbFile = new File(DB_PATH, DB_NAME);

		if (!dbFile.exists()) {
			return false;
		}

		SQLiteDatabase db = null;

		db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null,
				SQLiteDatabase.OPEN_READONLY
						| SQLiteDatabase.NO_LOCALIZED_COLLATORS);

		if (db != null) {
			db.close();
		}

		return db != null ? true : false;
	}

	private static void copyDataBase() throws IOException {
		InputStream myInput = AppContext.getInstance().getAssets()
				.open(ASSETS_NAME);

		String outFileName = DB_PATH + DB_NAME;
		OutputStream myOutput = new FileOutputStream(outFileName);

		byte[] buffer = new byte[1024];
		int length = 0;
		while ((length = myInput.read(buffer)) > 0) {
			myOutput.write(buffer, 0, length);
		}

		myOutput.flush();
		myOutput.close();
		myInput.close();
	}

	private static SQLiteDatabase getDatabase() {
		SQLiteDatabase db = null;

		db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null,
				SQLiteDatabase.NO_LOCALIZED_COLLATORS);

		return db;
	}

	// 选择城市界面需要城市列表
	public static HashMap<String, Integer> getAllCitys() {

		HashMap<String, Integer> map = null;
		SQLiteDatabase db = getDatabase();
		Cursor cursor = db.rawQuery("select city_id,city_name from city", null);

		int count = cursor.getCount();
		if (cursor.getCount() > 0) {
			map = new HashMap<String, Integer>();

			for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor
					.moveToNext()) {
				map.put(cursor.getString(cursor.getColumnIndex("city_name")),
						cursor.getInt(cursor.getColumnIndex("city_id")));
			}
		}

		cursor.close();
		db.close();
		return map;
	}

	// 根据城市名字获得城市id
	public static int getCityId(String cityName) {
		SQLiteDatabase db = getDatabase();

		Cursor cursor = null;

		try {
			cursor = db.rawQuery("select city_id from city where city_name='"
					+ cityName + "'", null);
		} catch (Exception e) {
			cursor.close();
			return -1;
		}

		cursor.moveToFirst();
		int test = cursor.getInt(cursor.getColumnIndex("city_id"));
		cursor.close();
		return test;
	}

	// 获得所有省
	public static Map<String, String> getAllProvince() {
		SQLiteDatabase db = getDatabase();

		Map<String, String> province = new HashMap<String, String>();
		Cursor cursor = null;
		cursor = db.rawQuery("select * from province", null);

		cursor.moveToFirst();
		int count = cursor.getCount();
		while (count-- > 0) {
			province.put(cursor.getString(cursor.getColumnIndex("prov_id")),
					cursor.getString(cursor.getColumnIndex("prov_name")));
			cursor.moveToNext();
		}

		return province;
	}

	// 根据省获得学校
	public static List<String> getSchoolsByProvince(String id) {
		SQLiteDatabase db = getDatabase();

		List<String> list = new ArrayList<String>();

		Cursor cursor = db.rawQuery(
				"select school_name from school where prod_id='" + id + "'",
				null);

		cursor.moveToFirst();
		int count = cursor.getCount();
		while (count-- > 0) {
			list.add(cursor.getString(cursor.getColumnIndex("school_name")));
			cursor.moveToNext();
		}

		return list;
	}
}
